package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.entity.*;
import cn.gjculture.shopping.api.service.RoutingRuleService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/routingRule")
public class RoutingRuleController {

    @Autowired
    RoutingRuleService routingRuleService;

    @ApiOperation(value = "新增分账规则", notes = "新增分账规则")
    @ApiImplicitParam(name = "routingRule", value = "分账规则对象", required = true, dataType = "Object")
    @PostMapping
    public Return save(String  param) {
        ParamData<List<RoutingRule>> g = JSONObject.parseObject(param, new TypeReference<ParamData<List<RoutingRule>>>() {});
        Return ret = new Return();
        if(routingRuleService.insert(g.getData())>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value = "获取分账规则", notes = "获取分账规则")
    @ApiImplicitParam(name = "goodsId", value = "分账规则ID", required = true, dataType = "String",paramType = "path")
    @GetMapping(value = "/{goodsId}")
    public Return<List<RoutingRule>> getRoutingRuleByGoodsId(@PathVariable(value = "goodsId") String goodsId) {
        Return<List<RoutingRule>> ret = new Return<>();
        List<RoutingRule> routingRule = routingRuleService.queryByGoodsId(goodsId);
        ret.setData(routingRule);
        return ret;
    }


    @ApiOperation(value = "删除分账规则", notes = "删除分账规则")
    @ApiImplicitParam(name = "routingRuleIds", value = "分账规则ID", required = true, dataType = "String",paramType = "path")
    @DeleteMapping(value = "/{routingRuleIds}")
    public Return<Activity> delete(@PathVariable(value = "routingRuleIds") List<String> routingRuleIds) {
        Return<Activity> ret = new Return<>();
        if(routingRuleService.deleteMore(routingRuleIds)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }


}
